package cn.ffcs.bpmpvc.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.echoice.util.DateUtil;

import cn.ffcs.bpm.model.TransactProcessStatus;
import cn.ffcs.bpmpvc.model.PocReqPlan;
import cn.ffcs.bpmpvc.services.AppBusinessReqService;
import cn.ffcs.bpmpvc.services.PocReqPlanService;
import cn.ffcs.bpmpvc.services.bean.ConfigBean;
import cn.ffcs.bpmpvc.svc.ProcessPvcService;
import cn.ffcs.bpmpvc.svc.TaskAssignBean;
import cn.ffcs.bpmpvc.svc.TaskService;
import cn.ffcs.bpmpvc.web.views.ProcessView;
import cn.ffcs.bpmpvc.web.views.TaskView;
import cn.ffcs.businessrequest.model.BusinessRequest;
import cn.ffcs.security.model.User;
import cn.ffcs.util.context.ApplicationContextUtil;

public class FlowReqDelayAction extends FlowAppBaseAction<TransactProcessStatus> {
	private ProcessPvcService processPvcService;
    private TaskService taskService;
    private AppBusinessReqService appBusinessReqService;
    private BusinessRequest businessRequest;
    private ConfigBean configBean;
	private PocReqPlanService pocReqPlanService;
	private String flowTaskId;
	private Integer procinstId;
	public String applyInit(){
		String processId=appBusinessReqService.getFlowReqDelayProcessId();
		ProcessView processView=getProcessPvcService().getInitProcessInfo(processId);
        ApplicationContextUtil.getHttpServletRequest().setAttribute("processView", processView);
		String reqId=ApplicationContextUtil.getContextObject("reqId");
		businessRequest=appBusinessReqService.getAppBusinessReqDao().getBusinessRequestById(Integer.valueOf(reqId));
		return  "toStart";
	}
	/**
	 * 启动流程
	 * @return
	 */
	public String saveStartFlow(){
		HttpServletRequest req=ApplicationContextUtil.getHttpServletRequest();
		List<TaskAssignBean> nextTasksList = getNextTaskUserList(req);
		String isCommitS=ApplicationContextUtil.getContextObject("isCommit");
		boolean isCommit=Boolean.valueOf(isCommitS);
		String reqId=req.getParameter("reqId");
		String delayTime=req.getParameter("delayTime");
		String delayAdv=req.getParameter("delayAdv");
		PocReqPlan pocReqPlan=new PocReqPlan();
		User user =ApplicationContextUtil.getUserContextObject().getCurrUser();
		if(isCommit){
			this.returnMsg="需求单延迟申请提交成功";
        }else{
        	this.returnMsg="需求单延迟申请保存成功";
        }
		try{
			String processId=appBusinessReqService.getFlowReqDelayProcessId();
			pocReqPlan.setReqId(reqId);
			pocReqPlan.setDelayTime(DateUtil.parse(delayTime, "yyyy-MM-dd"));
			pocReqPlan.setDelayAdv(delayAdv);
			pocReqPlan.setDelayUser(user.getPerson().getName());
			pocReqPlan.setDelayOptime(new Date());
			getPocReqPlanService().saveReqPlanStartFlow(processId, user.getPerson(), nextTasksList, isCommit, pocReqPlan);
		}catch (Exception e) {
			// TODO: handle exception
			this.returnMsg=e.getMessage();
		}
		return  SUCCESS;
	}
	
	public String showDetail(){
		//根据流程实例ID取得延迟申请信息
		
		HttpServletRequest req=ApplicationContextUtil.getHttpServletRequest();
		PocReqPlan reqPlan=getPocReqPlanService().getPocReqPlanDao().getReqPlan(procinstId);
		if(reqPlan!=null){
			Integer reqId=Integer.valueOf(reqPlan.getReqId());
			businessRequest=getAppBusinessReqService().getAppBusinessReqDao().getBusinessRequestById(reqId);
		}
		req.setAttribute("reqPlan", reqPlan);
		TaskView taskView=getTaskService().getTaskView(flowTaskId);
		ApplicationContextUtil.getHttpServletRequest().setAttribute("taskView", taskView);
		String toPage=ApplicationContextUtil.getContextObject("toPage");
		if(StringUtils.isNotBlank(toPage)){
			return toPage;
		}
		return  "detail";
	}


	public String saveOrCommitTask(){
		User user =ApplicationContextUtil.getUserContextObject().getCurrUser();
		String procinstId=ApplicationContextUtil.getContextObject("procinstId");
        String isCommitS=ApplicationContextUtil.getContextObject("isCommit");
        String confirmType=ApplicationContextUtil.getContextObject("confirmType");
        boolean isCommit=Boolean.valueOf(isCommitS);//保存
        HttpServletRequest req=ApplicationContextUtil.getHttpServletRequest();
        List<TaskAssignBean> nextTasksList = getNextTaskUserList(req);
        String content=req.getParameter("commentComtent");
        setCommitTaskMsg(isCommit);
        try{
        	PocReqPlan reqPlan=getPocReqPlanService().getPocReqPlanDao().getReqPlan(Integer.valueOf(procinstId));
        	reqPlan.setConfirmAdv(content);
        	reqPlan.setConfirmOptime(new Date());
        	reqPlan.setConfirmType(confirmType);
        	reqPlan.setConfirmUser(user.getPerson().getName());
        	getPocReqPlanService().saveDataAndCommitTask(flowTaskId, content, reqPlan, nextTasksList, isCommit);
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            returnMsg=e.getMessage();
        }
        return SUCCESS;
	}
	
	public ProcessPvcService getProcessPvcService() {
		return processPvcService;
	}

	public void setProcessPvcService(ProcessPvcService processPvcService) {
		this.processPvcService = processPvcService;
	}

	public TaskService getTaskService() {
		return taskService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public AppBusinessReqService getAppBusinessReqService() {
		return appBusinessReqService;
	}

	public void setAppBusinessReqService(AppBusinessReqService appBusinessReqService) {
		this.appBusinessReqService = appBusinessReqService;
	}

	public BusinessRequest getBusinessRequest() {
		return businessRequest;
	}

	public void setBusinessRequest(BusinessRequest businessRequest) {
		this.businessRequest = businessRequest;
	}

	public ConfigBean getConfigBean() {
		return configBean;
	}

	public void setConfigBean(ConfigBean configBean) {
		this.configBean = configBean;
	}
	public PocReqPlanService getPocReqPlanService() {
		return pocReqPlanService;
	}
	public void setPocReqPlanService(PocReqPlanService pocReqPlanService) {
		this.pocReqPlanService = pocReqPlanService;
	}
	public String getFlowTaskId() {
		return flowTaskId;
	}
	public void setFlowTaskId(String flowTaskId) {
		this.flowTaskId = flowTaskId;
	}
	public Integer getProcinstId() {
		return procinstId;
	}
	public void setProcinstId(Integer procinstId) {
		this.procinstId = procinstId;
	}
	
	
}
